package com.five.taotao.guide_mine;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import com.five.taotao.Adapter.GoodsPayAdapter;
import com.five.taotao.Application.MyApplication;
import com.five.taotao.R;
import com.five.taotao.entity.Order;
import com.five.taotao.entity.ShoppingCart;
import com.five.taotao.selfwidget.CustomProgressDialog;
import com.five.taotao.util.Constants;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * A simple {@link Fragment} subclass.
 */
public class PayOrderFragment extends Fragment {

    @BindView(R.id.rv)
    ListView lv;
    private List<Order.ListBean> mData;
    private GoodsPayAdapter adapter;
    private CustomProgressDialog myDialog;

    public PayOrderFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_pay_order, container, false);
        ButterKnife.bind(this, view);
        mData = new ArrayList<>();
        adapter = new GoodsPayAdapter(getActivity(), mData);
        lv.setAdapter(adapter);
        loadData();

        return view;
    }


    public void loadData() {
        circle();
        OkHttpUtils
                .post()
                .url(Constants.O_URL)
                .addParams("id", MyApplication.userId)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        MyApplication.toast.toastShow("订单查询失败");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        Order order = gson.fromJson(response, Order.class);
                        List<Order.ListBean> items = order.getList();
                        mData.clear();
                        mData.addAll(items);
                        adapter.notifyDataSetChanged();
                        myDialog.dismiss();
                    }
                });
    }

    /**
     * 圆形进度条测试..
     */
    public void circle() {
        myDialog = new CustomProgressDialog(getActivity(),R.style.dialog);
        Window wd= myDialog.getWindow();
        WindowManager.LayoutParams lp = wd.getAttributes();
        lp.alpha = 0.9f;
        wd.setAttributes(lp);
        myDialog.show(); // 显示进度条
    }

}
